#!/usr/bin/env python
# -*- coding: utf8 -*-

'''
Created on 2011-8-2

@author: zhongfeng
'''
from pageparser import ObuyUrlSummary
from amazon.amazonpageparser import parserDict,rootUrlSummary
from spider import ObuySpider,main
import os,sys
from logfacade import LoggerFactory

class AmazonSpider(ObuySpider):
    def __init__(self, rootUrlSummary=None, parserDict=None, threadNum=5,
                 procDetails=True, include=None, exclude=None, rootPageResult=None,):
        super(AmazonSpider, self).__init__(rootUrlSummary, parserDict, threadNum,
                 procDetails, include, exclude, rootPageResult)
        
    def init_urls(self):
        curPath = os.path.abspath(os.path.dirname(sys.argv[0]))
        catFile = os.path.join(curPath,'amazon.cat')
        with open(catFile) as f:
            for line in f:
                name,url,level =  [t.decode('utf-8') for t in line.split(',')]
                self.putSpideRequest(ObuyUrlSummary(name=name,url=url,catagoryLevel=int(level)))
                
    def procParserResult(self, result, urlsum, parser):
        if urlsum.catagoryLevel == 3:#作为最终页面的标志
            urlsum.parent = urlsum
        parserResult = parser.parserSubUrlSums()
        if parserResult:
            for subUrlSum in parserResult:
                self.putSpideRequest(subUrlSum)
        else:
            if urlsum.catagoryLevel == 2:
                urlsum.catagoryLevel = 3
            self.putSpideRequest(urlsum)
        self.procPageInfos(parser,urlsum)

if __name__ == '__main__':
    main(root=rootUrlSummary,parserDict=parserDict,SpiderClass = AmazonSpider)
